package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Banji;
import com.situ.web.entity.Student;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.JSONUtil;
import com.situ.web.vo.StudentBanjiVO;


@WebServlet("/student")
public class StudentServlet extends HttpServlet{
	StudentServiceImpl studentService = new StudentServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 解决post请求乱码问题  放在最前面
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		if(method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch(method) {
		case "selectBanjiName":
			selectBanjiName(req, resp);
			break;
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectName":
			selectName(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectId":
			selectId(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}
	private void selectBanjiName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Banji> list = studentService.selectBanjiName();
		
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/student_insert.jsp").forward(req, resp);
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}
	private void selectName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String selectName = req.getParameter("selectName");
		ArrayList<StudentBanjiVO> list = studentService.selectName(selectName);
		
		
		// 把数据放到request里面，就相当于一块内存区域
		req.setAttribute("list", list);
		// 转发（跳转）到student_list.jsp页面进行展示
		req.getRequestDispatcher("student_list.jsp").forward(req, resp);
	}
	
	private void selectId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String selectId = req.getParameter("id");
		Student student = studentService.selectId(Integer.parseInt(selectId));
		
		
		req.setAttribute("student", student);
		req.getRequestDispatcher("student_update.jsp").forward(req, resp);
//		// 把list集合数据放到request里面，就相当于一块内存区域
//		req.setAttribute("student", student);
//		// 转发（跳转）到student_list.jsp页面进行展示
//		req.getRequestDispatcher("student_update.jsp").forward(req, resp);
	}
	
	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.service()");
		ArrayList<StudentBanjiVO> list = studentService.selectAll();
		
		
		// 把list集合数据放到request里面，就相当于一块内存区域
		req.setAttribute("list", list);
		// 转发（跳转）到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
//		System.out.println(id);
		boolean isSuccess = studentService.delete(Integer.parseInt(id));
		System.out.println(isSuccess);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		map.put("msg", isSuccess ? "删除成功" : "删除失败");
		JSONUtil.obj2Json(map, resp);
		// 删除完了之后重定向到查找所有界面 /studentSelect
		// req.getContextPath()得到该项目的名字
//		resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}
	
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		if(id == "") {
			resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
			return;
		}
		if(age == "") {
			age = "0";
		}
		StudentBanjiVO student = new StudentBanjiVO(Integer.parseInt(id), name, Integer.parseInt(age), gender, Integer.parseInt(banjiId));
		boolean isSuccess = studentService.insert(student);
		System.out.println(isSuccess);
		
		
		// req.getContextPath()得到该项目的名字
		resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}
		
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		StudentBanjiVO student = new StudentBanjiVO(Integer.parseInt(id), name, Integer.parseInt(age), gender, Integer.parseInt(banjiId));
		boolean isSuccess = studentService.update(student);
		System.out.println(isSuccess);

		resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}


}
